package com.ll.service.impl;

import com.ll.dao.entity.EmailServer;
import com.ll.dao.mapper.EmailServerMapper;
import com.ll.dao.mapper.EmailServerMapperExt;
import com.ll.exception.UserException;
import com.ll.exception.UserExceptionConstants;
import com.ll.service.EmailServerService;
import com.ll.utils.DesUtil;
import com.ll.utils.StringUtil;
import com.ll.vo.request.EmailServerRequestVo;
import com.ll.vo.response.EmailServerResponseVo;
import com.ll.vo.response.PrimaryIdVo;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * 邮件服务端的操作，包括添加、删除、修改、查询
 * 
 * @author liudawei
 *
 */
@Service
public class EmailServerServiceImpl implements EmailServerService {

	private static final Logger LOGGER = LoggerFactory.getLogger(EmailServerServiceImpl.class);
	@Autowired
	private EmailServerMapper emailServerMapper;
	@Autowired
	private EmailServerMapperExt emailServerMapperExt;
	@Value("${system.allowIP}")
	private String allowIP;
	/**
	 * 新增邮件服务端
	 * 
	 * @param emailServerRequestVo
	 * @return
	 */
	@Override
	public PrimaryIdVo saveEmailServer(EmailServerRequestVo emailServerRequestVo) {
		EmailServer emailServer = new EmailServer();
		String id = StringUtil.createUUID();
		try {
			BeanUtils.copyProperties(emailServer, emailServerRequestVo);
			emailServer.setId(id);
		} catch (IllegalAccessException e) {
			LOGGER.error("EmailServerRequestVo对象和EmailServer对象在拷贝属性值的时候，抛出异常信息", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		} catch (InvocationTargetException e) {
			LOGGER.error("EmailServerRequestVo对象和EmailServer对象在拷贝属性值的时候，抛出异常信息", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
		try {
			emailServer.setPassword(DesUtil.encrypt(emailServerRequestVo.getPassword(), emailServerRequestVo.getAccount()));
			if (emailServerRequestVo.getPort() == null) {
				emailServer.setPort(25);
			}
			emailServerMapper.insertEmailServer(emailServer);
			return new PrimaryIdVo(id);
		} catch (Exception e) {
			LOGGER.error("新增邮件服务端信息时报错", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
	}

	/**
	 * 修改邮件服务端
	 * 
	 * @param emailServerRequestVo
	 */
	@Override
	public void updateEmailServer(EmailServerRequestVo emailServerRequestVo) {
		EmailServer emailServer;
		try {
			emailServer = emailServerMapper.findEmailServerById(emailServerRequestVo.getId());
		} catch (Exception e) {
			LOGGER.error("查询邮件服务端信息时报错", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
		if (emailServer == null) {
			LOGGER.error("数据库中返回的数据为NULL,程序抛出一个异常");
			throw new UserException(UserExceptionConstants.NULL);
		}
		try {
			BeanUtils.copyProperties(emailServer, emailServerRequestVo);
		} catch (IllegalAccessException e) {
			LOGGER.error("EmailServerRequestVo对象和EmailServer对象在拷贝属性值的时候，抛出异常信息", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		} catch (InvocationTargetException e) {
			LOGGER.error("EmailServerRequestVo对象和EmailServer对象在拷贝属性值的时候，抛出异常信息", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
		try {
			emailServer.setPassword(DesUtil.encrypt(emailServerRequestVo.getPassword(), emailServerRequestVo.getAccount()));
			if (emailServerRequestVo.getPort() == null) {
				emailServer.setPort(25);
			}
			emailServerMapper.updateEmailServer(emailServer);
		} catch (Exception e) {
			LOGGER.error("修改邮件服务端信息时报错", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
	}

	/**
	 * 删除邮件服务端
	 * 
	 * @param id
	 */
	@Override
	public void deleteEmailServer(String id) {
		try {
			emailServerMapper.deleteEmailServerById(id);
		} catch (Exception e) {
			LOGGER.error("删除邮件服务端信息时报错", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
	}


	/**
	 * 根据ID查询
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public EmailServerResponseVo findById(String id) {
		EmailServer emailServer;
		try {
			emailServer = emailServerMapper.findEmailServerById(id);
		} catch (Exception e) {
			LOGGER.error("查询邮件服务端信息时报错", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
		if (emailServer == null) {
			LOGGER.error("数据库中返回的数据为NULL,程序抛出一个异常");
			throw new UserException(UserExceptionConstants.NULL);
		}
		EmailServerResponseVo emailServerResponseVo = new EmailServerResponseVo();
		try {
			BeanUtils.copyProperties(emailServerResponseVo, emailServer);
			try {
				emailServerResponseVo.setPassword(DesUtil.decrypt(emailServerResponseVo.getPassword(), emailServerResponseVo.getAccount()));
			} catch (IOException e) {
				LOGGER.error("密码解密出错");
			} catch (Exception e) {
				LOGGER.error("密码解密出错");
			}
		} catch (IllegalAccessException e) {
			LOGGER.error("EmailServerResponseVo对象和EmailServer对象在拷贝属性值的时候，抛出异常信息", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		} catch (InvocationTargetException e) {
			LOGGER.error("EmailServerResponseVo对象和EmailServer对象在拷贝属性值的时候，抛出异常信息", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
		return emailServerResponseVo;
	}

	@Override
	public EmailServerResponseVo findByShopId(String shopId) {
		EmailServerResponseVo emailServerResponseVo = new EmailServerResponseVo();
		EmailServer emailServer;
		try {
			emailServer = emailServerMapperExt.findEmailServerByShopId(shopId);
		} catch (Exception e) {
			LOGGER.error("查询邮件服务端信息时报错", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
		if (emailServer == null) {
			return null;
		}
		try {
			BeanUtils.copyProperties(emailServerResponseVo, emailServer);
		} catch (IllegalAccessException e) {
			LOGGER.error("EmailServerResponseVo对象和EmailServer对象在拷贝属性值的时候，抛出异常信息", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		} catch (InvocationTargetException e) {
			LOGGER.error("EmailServerResponseVo对象和EmailServer对象在拷贝属性值的时候，抛出异常信息", e);
			throw new UserException(UserExceptionConstants.MAINTAINING, e);
		}
		return emailServerResponseVo;
	}


}
